package com.yunyike.service.impl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yunyike.exception.BusinessException;
import com.yunyike.mapper.QuotationBrandMappingMapper;
import com.yunyike.model.Brand;
import com.yunyike.model.QuotationBrandMapping;
import com.yunyike.service.BrandService;
import com.yunyike.service.QuotationBrandMappingService;

@Service("quotationBrandMappingServiceImpl")
public class QuotationBrandMappingServiceImpl implements QuotationBrandMappingService{

	private Logger log = LoggerFactory.getLogger(QuotationBrandMappingServiceImpl.class);

	@Autowired
	private QuotationBrandMappingMapper quotationBrandMappingMapper;

	@Autowired
	private BrandService brandService;
	
	public int save(String [] brand, String quotationId) {
		
		int result = 0;
		if(brand == null || brand.length < 1|| StringUtils.isBlank(quotationId)){
			log.info("QuotationBrandMappingServiceImpl save 请求参数不正确。");
			return result;
		}
		
		try {
			for (String brandId : brand) {
				
				QuotationBrandMapping qbm = new QuotationBrandMapping();
				qbm.setQuotationBrandId(UUID.randomUUID().toString());
				qbm.setQuotationId(quotationId);
				qbm.setBrandId(brandId);
				
				result = quotationBrandMappingMapper.insert(qbm);
				log.info("quotationId:{},brandId:{},saveResult:{}",quotationId,brandId,result);
			}
		} catch (Exception e) {
			throw new BusinessException(e.getMessage(),e);
		}
		
		return result;
	}

	@Override
	public List<QuotationBrandMapping> findQuotationBrandByQutationId(String quotationId) {

		if(StringUtils.isBlank(quotationId)){
			return null;
		}
		return quotationBrandMappingMapper.findQuotationBrandByQuotationId(quotationId);
	}

	@Override
	public Map<String, List<QuotationBrandMapping>> findQuotationBrandByQutationId(List<String> quotationId) {
		
		Map<String, List<QuotationBrandMapping>> returnMap = new HashMap<String, List<QuotationBrandMapping>>();
		
		if(quotationId == null || quotationId.isEmpty()){
			return returnMap;
		}
		
		for (String string : quotationId) {
			if(StringUtils.isBlank(string)){
				continue;
			}
			
			List<QuotationBrandMapping> list = quotationBrandMappingMapper.findQuotationBrandByQuotationId(string);
			if(list == null || list.isEmpty()){
				log.info("quotationId:{} findQuotationBrand return null.",string);
				continue;
			}
			returnMap.put(string, list);
		}
		
		if(returnMap.isEmpty()){
			return null;
		}
		// 设置品牌名称
		Iterator<Entry<String, List<QuotationBrandMapping>>> iterator = returnMap.entrySet().iterator();
		while(iterator.hasNext()){
			Entry<String, List<QuotationBrandMapping>> next = iterator.next();
			List<QuotationBrandMapping> value = next.getValue();
			
			for (QuotationBrandMapping quotationBrandMapping : value) {
				
				String brandId = quotationBrandMapping.getBrandId();
				Brand brand = brandService.findBrandById(brandId);
				
				quotationBrandMapping.setBrandId(brand.getBrandName());
			}
		}
		
		return returnMap;
	}
	
	
}
